using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using DTO;
using System.Configuration;
using System.Data;
namespace DAL
{ 
    public class KHACHHANG_DAL
    {
        private SqlConnection cn = null;
        public KHACHHANG_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"]
                 .ConnectionString);
            cn.Open();
          
        }
        public void insert(KhachHang_DTO kh)
        {
            SqlCommand cmd = new SqlCommand("KHACHHANG_INSERT", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@HOTEN",kh.HoTen);
            cmd.Parameters.AddWithValue("@MALOAI_NGUOIDUNG",kh.MaLoai_NguoiDung);
            cmd.Parameters.AddWithValue("@EMAIL", kh.Email);
            cmd.Parameters.AddWithValue("@MATKHAU", kh.MatKhau);
            cmd.Parameters.AddWithValue("@DIACHI", kh.DiaChi);
            cmd.Parameters.AddWithValue("@DIENTHOAI", kh.DienThoai);
            cmd.Parameters.AddWithValue("@HINHANH", kh.HinhAnh);
            cmd.Parameters.AddWithValue("@GIOITINH", kh.GioiTinh);
            cmd.ExecuteNonQuery();
        }
        public void update(KhachHang_DTO kh)
        {
            SqlCommand cmd = new SqlCommand("KHACHHANG_UPDATE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MAKH", kh.MaKH);
            cmd.Parameters.AddWithValue("@HOTEN", kh.HoTen);
            cmd.Parameters.AddWithValue("@MALOAI_NGUOIDUNG", kh.MaLoai_NguoiDung);
            cmd.Parameters.AddWithValue("@EMAIL", kh.Email);
            cmd.Parameters.AddWithValue("@MATKHAU", kh.MatKhau);
            cmd.Parameters.AddWithValue("@DIACHI", kh.DiaChi);
            cmd.Parameters.AddWithValue("@DIENTHOAI", kh.DienThoai);
            cmd.Parameters.AddWithValue("@HINHANH", kh.HinhAnh);
            cmd.Parameters.AddWithValue("@GIOITINH", kh.GioiTinh);
            cmd.ExecuteNonQuery();
        }
        public void delete(int makh)
        {
            SqlCommand cmd = new SqlCommand("KHACHHANG_DELETE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MAKH",makh);
            cmd.ExecuteNonQuery();
        }
        public tbKhachHangCollection Get(String where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("KHACHHANG_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@WHERE", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PAGESIZE", pageSize);
            cm.Parameters.AddWithValue("@ORDERBY", orderBy);
            cm.Parameters.AddWithValue("@ORDERDIRECTION", orderDirection);

            //cm.Parameters .Add (new SqlParameter ("@TOTALRECORD",DbType.Int32)).Direction=ParameterDirection.Output 
            cm.Parameters.Add(new SqlParameter("@TOTALRECORDs", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbKhachHangCollection KHCollection = new tbKhachHangCollection();
            while (dr.Read())
            {
                KhachHang_DTO kh = new KhachHang_DTO();
                kh.MaKH = int.Parse(dr.GetValue(0).ToString ());
                kh.HoTen = dr.GetValue(1).ToString();
                kh.MaLoai_NguoiDung = int.Parse(dr.GetValue (2).ToString ());
                kh.Email = dr.GetValue(3).ToString();
                kh.MatKhau = dr.GetValue(4).ToString();
                kh.DiaChi = dr.GetValue(5).ToString();
                kh.DienThoai = dr.GetValue(6).ToString();
                kh.HinhAnh = dr.GetValue(7).ToString();
                kh.GioiTinh = Boolean.Parse(dr.GetValue (8).ToString ());
                KHCollection.Add(kh);


            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TOTALRECORDs"].Value.ToString());
            return KHCollection;


        }
    }
}
